<?php
/**
 * Created by xiaosong
 * E-mail:306027376@qq.com
 * Date: 2018/11/14
 * Time: 10:16
 */

namespace app\admin\controller;
use app\common\logic\Logic;
use app\common\logic\SkillForm;
use app\common\logic\SkillTag;
use app\common\model\Order;
use app\common\model\Skill as model;
use app\common\model\SkillApply;
use Qiniu\Auth;
use think\Db;

class Skill extends Base
{

    /**
     * Created by xiaosong
     * E-mail:306027376@qq.com
     * 技能列表
     */
    public function index()
    {

        $map = [];

        if (input('skill_name')) $map['skill_name'] = ['like','%'.trim(input('skill_name')).'%'];

        if(!isEmpty($_GET['type'])) $map['type'] = trim(input('type'));

        $model = new model();
        $rows  = $model->getList($map);
        $this->assign([
            'title' => '技能列表',
            'rows' => $rows,
            'pageHTML' => $rows->render(),
        ]);

        return view();

    }

    /**
     * 订单列表
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function order_list()
    {
        $map = [];

        if(!empty($_GET['start'])&&empty($_GET['end'])){
            $map['a.create_time'] = ['>=',strtotime($_GET['start'])];
        }elseif(!empty($_GET['end'])&&empty($_GET['start'])){
            $map['a.create_time'] = ['<=',strtotime($_GET['end'])];
        }elseif(!empty($_GET['start'])&&!empty($_GET['end'])){
            $map['a.create_time'] = ['between',array(strtotime($_GET['start']),strtotime($_GET['end']))];
        }

        if (is_numeric(input('uuid'))){
            $map['u.uuid'] =  ['like','%'.trim(input('get.uuid')).'%'];
        }

        if (is_numeric(input('sd'))){
            $map['us.uuid'] =  ['like','%'.trim(input('get.sd')).'%'];
        }

        $model = new Order();

        $rows  = $model->getOrder($map);

        $total = $model->alias('a')->sum('price');

        $map['a.status'] = ['in',[3,4]];

        //完成订单
        $finish = $model->alias('a') ->join('skill s','s.skill_id = a.skill_id','LEFT')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->join('users us','us.user_id = a.to_user','LEFT')->where($map)->count();

        //进行中
        $map['a.status'] = ['in',[1,2]];
        $hand = $model->alias('a') ->join('skill s','s.skill_id = a.skill_id','LEFT')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->join('users us','us.user_id = a.to_user','LEFT')->where($map)->count();

        //废弃订单式
        $map['a.status'] = ['between',[5,9]];
        $wrong = $model->alias('a') ->join('skill s','s.skill_id = a.skill_id','LEFT')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->join('users us','us.user_id = a.to_user','LEFT')->where($map)->count();

        $this->assign([
            'title' => '技能列表',
            'rows' => $rows,
            'pageHTML' => $rows->render(),
            'total' => $total,
            'finish' => $finish,
            'hand' => $hand,
            'wrong' => $wrong,
        ]);

        return view();


    }



    /**
     * Created by xiaosong
     * E-mail:306027376@qq.com
     * 技能编辑
     */
    public function edit()
    {
        $table = 'skill';
        $data = input();
        if (request()->isPost()){
            $model = new Logic();
            $result = $model->saveChange($table,$data,'skill');

            if ($result){
                if (is_numeric($data['id'])){
                    $skill = model::get($data['id']);
                }else{
                    $id = $model->getLastInsID();
                    $skill = model::get($id);
                }
                //$skill->skillForm()->delete(true);
                foreach ($data['form_name'] as $k=> $v){
                    if($v)
                    {
                        $arr['name'] = $v;
                        $arr['price'] = $data['form_price'][$k];
                        $arr['skill_id']  = $skill['skill_id'];
                        if($data['form_id'][$k])
                        {
                            $skill->skillForm()->where('id',$data['form_id'][$k])->update($arr);
                        }else{
                            $skill->skillForm()->save($arr);
                        }
                    }
                }
                $this->success('操作成功',url('index'));
            }
            $this->error($model->getError());
        }

        $auth  = new Auth(config('qiniu')['ACCESSKEY'], config('qiniu')['SECRETKEY']);
        $token = $auth->uploadToken(config('qiniu')['bucket']);
        $domain = config('qiniu')['domain'];

        $data = model::get($data['id']);
        $this->assign('data',$data);
        $this->assign([
            'title' => '技能编辑',
            'token' => $token,
            'domain'=> $domain,
        ]);

        return view();
    }


    /**
     * Created by xiaosong
     * E-mail:306027376@qq.com
     * 更改状态
     */
    public function change()
    {
        $this->_change('skill');
    }


    /**
     * Created by xiaosong
     * E-mail:306027376@qq.com
     * 用户技能删除
     */
    public function del()
    {
        $this->_del('skill_apply');
    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 资质申请列表
     */
    public function apply()
    {

        $map = [];

        if (input('skill_name')) $map['s.skill_name'] = ['like','%'.trim(input('skill_name')).'%'];

        if(!isEmpty($_GET['type'])) $map['s.type'] = trim(input('type'));

        if (!isEmpty(input('status'))){
            $map['a.status'] = trim(input('status'));
        }

        if (is_numeric(input('uuid'))){
            $map['u.uuid'] =  ['like','%'.trim(input('get.uuid')).'%'];
        }


        $model = new SkillApply();
        $rows  = $model->getList($map);
        $this->assign([
            'title' => '技能列表',
            'rows' => $rows,
            'pageHTML' => $rows->render(),
        ]);

        return view();
    }


    
    
    
    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 资质申请审核
     */
    public function apply_edit()
    {

        if (request()->isPost()){
            $data = input('post.');

            if (!is_numeric($data['id'])){
                $this->error('参数错误');
            }
            $result = Db::name('skill_apply')->where('apply_id',$data['id'])->update(['status'=>$data['status']]);
            $model = new SkillApply();
            $skill_apply = $model->getDetail(['a.apply_id'=>input('id')]);
            if ($result){

                if ($data['status'] == 1){
                    //审核通过
                    $str = sprintf('您申请的技能 %s 已通过审核,现在开始接单吧!',$skill_apply['skill_name']);
                }else{
                    //审核失败
                    if (!empty($data['remark'])){
                        $remark = ',驳回原因为:'.$data['remark'];
                    }else{
                        $remark = '.';
                    }
                    $str = sprintf('您申请的技能 %s 未通过审核%s',$skill_apply['skill_name'],$remark);
                }
                \app\api\controller\Base::sendUsrMsg('system',hashid($skill_apply['user_id']),$str);

                $this->success('操作成功',url('apply'));
            }
            $this->error('操作失败');

        }


        $model = new SkillApply();

        $data = $model->getDetail(['a.apply_id'=>input('id')]);

        $this->assign([
            'title' => '技能申请编辑',
            'data' => $data,
        ]);
        return view();

    }


    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 订单申诉
     */
    public function protest()
    {

        $map = [];
        $map['a.status'] = 9;
        $model = new Order();

        $rows = $model->getList($map);
        $this->assign([
            'title' => '订单申诉列表',
            'rows' => $rows,
            'pageHTML' => $rows->render(),
        ]);

        return view();
    }

    /**
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * 申诉审核
     */
    public function protest_edit()
    {
        if (request()->isPost()){
            $data = input('post.');
            $map['order_id'] = $data['id'];
            $map['status']   = 9;
            $order = Db::name('order')->where($map)->find();

            if (!$order) api_return(0,'申诉订单不存在');

            if ($data['status'] == 1){ //申诉成功

                $result = \app\api\controller\Base::refund($data['id'],11);

                if ($result === true){
                    $this->success('操作成功',url('protest'));
                }else{
                    $this->error($result);
                }

            }else{ //申诉失败
                $result = Db::name('order')->where('order_id',$data['id'])->update(['status'=>10,'update_time'=>time()]);

                if ($result !== false){
                    $this->success('操作成功',url('protest'));
                }else{
                    $this->error('操作失败');
                }

            }





        }

        $model = new Order();

        $map['a.order_id'] = input('id');
        $data  = $model->getDetail($map);
        $data['protest'] = \app\api\controller\Base::staticInfo('nick_name,header_img',$data['user_id']);
        $data['user'] = \app\api\controller\Base::staticInfo('nick_name,header_img',$data['to_user']);
//        dd($data);
        $this->assign([
            'title' => '申诉审核',
            'data' => $data,
        ]);
        return view();

    }



}